package cn.exrick.xboot.modules.app.controller;

import cn.exrick.xboot.common.utils.HttpClientUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.hutool.core.util.StrUtil;
import io.netty.channel.ConnectTimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/xboot/app")
public class SsoLoginController {

    @PostMapping(value = "/sso")
    public Result<Object> appSSO(String templateName, String infoID, String url,
                                 HttpServletRequest request, HttpServletResponse response){
        log.info("templateName={}---infoID={}----url={}---accessToken={}",templateName,infoID,url,request.getHeader("accessToken"));
        if(StrUtil.isBlank(templateName)|| StrUtil.isBlank(url)){
            return new ResultUtil<>().setErrorMsg("缺少必需表单字段");
        }
        Map map = new HashMap<>();

        map.put("templateName",templateName);
        map.put("infoID",infoID);
        map.put("authToken",request.getHeader("accessToken"));

        String requestMethod = "post";
        String responseURL= null;
        try{
            responseURL =  HttpClientUtil.httpRequestToString(url,requestMethod,map,null);
            //responseURL = HttpClientUtil.postForm(url,map);
        }catch(ConnectTimeoutException e) {
            // 捕获超时异常 并反馈给调用者
            e.printStackTrace();
            return new ResultUtil<>().setErrorMsg("connection time out");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>().setErrorMsg("Connection exception");
        }
        if (responseURL == null) {
            log.error("对方系统返回数据为空！");
            return new ResultUtil<>().setErrorMsg("对方系统返回数据为空！");
        }
        return new ResultUtil<>().setData(responseURL);
    }

    /*@RequestMapping("/clientToHtml")
    public HttpServletResponse clientToHtml(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String body = HttpUtils.getData("http://www.baidu.com");//body为获取的html代码
        *//*Document doc = Jsoup.parse(body);
        LOGGER.info("返回页面内容：",doc.outerHtml());*//*
        response.setContentType("text/html;charset=utf-8");
       *//* PrintWriter out = response.getWriter();*//*
        return response;
    }

    @RequestMapping("/clientToHtml2")
    public HttpServletResponse clientToHtml2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map map = new HashMap<>();

        map.put("templateName","");
        map.put("infoID","");
        String body = HttpClientUtil.httpRequestToString("http://www.baidu.com","get",map,null);//body为获取的html代码
        *//*Document doc = Jsoup.parse(body);
        LOGGER.info("返回页面内容：",doc.outerHtml());*//*
        response.setContentType("text/html;charset=utf-8");
        *//* PrintWriter out = response.getWriter();*//*
        log.info("测试log打印日志{}",body);
        return response;
    }*/
}
